﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;

public partial class TotalDrugs_ExpensivedrugsStatistics : BasePage
{
    public TotalDrugs_ExpensivedrugsStatistics()
    { }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();//绑定数据
            QueryControl1.BindSort(gvwInfo);
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
    }

    /// <summary>
    /// 绑定符合条件的数据
    /// </summary>
    private void BindData()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            //分组字段
            GroupField = GroupByField.IsPrecious,
            //查询条件
            Query = (SuperviseQuery)ViewState["query"],
            //选择分组字段
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.WareHouseRate : ShowColumns.ShowColumn.GroupFields,
            //排序字段
            SortField = QueryControl1.SortField == "" ? "IsPrecious ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        AspNetPager1.RecordCount = t;

        GridViewUtil.AdjustGridView(gvwInfo, dt);
        this.gvwInfo.DataSource = dt;
        this.gvwInfo.DataBind();
        setChart(dt);
        SetColumnVisible();
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    /// <summary>
    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {
        if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnDrug").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnCom_PS").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
        {
            foreach (GridViewRow row in gvwInfo.Rows)
            {
                row.FindControl("lbnHos").Visible = false;
            }
        }
    }


    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;
        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.SortName = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }
    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Hospital")
        {
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.CountOrder;

            }


            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.IsPrecious = bool.Parse(e.CommandArgument.ToString());
            Session[sessionid] = query;

            string url = System.Configuration.ConfigurationManager.AppSettings["HospitalID"];

            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        if (e.CommandName == "Company")
        {
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.CountOrder;

            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.IsPrecious = bool.Parse(e.CommandArgument.ToString());
            Session[sessionid] = query;

            string url = System.Configuration.ConfigurationManager.AppSettings["CompanyID_PS"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }

        if (e.CommandName == "Drugs")
        {
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal;

            }

            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.IsPrecious = bool.Parse(e.CommandArgument.ToString());
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "贵重药品";
            serChart.DataSource = dt;
            serChart.XName = "IsPrecious";
            serChart.YNames = "PurchaseAmountTotal";

            if (ShowColumns.ShowColumn == null)
            {
                serChart.XName = "IsPrecious";
                serChart.YNames = "PurchaseAmountTotal";
            }
            else
            {
                string[] _columns = ShowColumns.ShowColumn.GroupFields.ToString().Split(',');
                serChart.XName = "IsPrecious";
                serChart.YNames = _columns[0];
            }

            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

            ChartControl1.webChart = serChart;
            ChartControl1.PageId = Guid.NewGuid().ToString();
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
    //protected void btnExcel_Click(object sender, EventArgs e)
    //{
    //    int t = 0;
    //    HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
    //    ie.QueryObject = new HZ.Supervise.Model.QueryObject()
    //    {
    //        //分组字段
    //        GroupField = GroupByField.IsPrecious,
    //        //查询条件
    //        Query = (SuperviseQuery)ViewState["query"],
    //        //选择分组字段
    //        GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.WareHouseRate : ShowColumns.ShowColumn.GroupFields,
    //        //排序字段
    //        SortField = QueryControl1.SortField == "" ? "IsPrecious ASC" : QueryControl1.SortField
    //    };

    //    System.Data.DataTable dt = ie.ExecutePage(0, 0, out t); 

    //    ExportFile.Instance.ExportData(dt, gvwInfo, this.Page); 
    //}
}
